package com.aiyige.base.api.httpinterceptor;

import android.text.TextUtils;

import com.aiyige.base.api.ApiUrls;
import com.aiyige.base.api.ResponseCode;
import com.aiyige.base.eventbus.EventAddFollow;
import com.aiyige.base.eventbus.EventDeleteFollow;
import com.aiyige.model.AiyigeExceptionModel;
import com.aiyige.model.sns.Follow;
import com.aiyige.utils.crash.CrashUtils;
import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;

public class FollowInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        try {
            Response response = chain.proceed(request);
            if (request.url().toString().contains("sns/userFollows") && request.method().equals("POST") && response.code() == ResponseCode.CREATED) {
                //处理添加关注成功
                String respjson = response.body().string();
                response = response.newBuilder()
                        .body(ResponseBody.create(MediaType.parse("text/plain"), respjson))
                        .build();
                try {
                    Follow respfollow = JSON.parseObject(respjson, Follow.class);
                    String body = bodyToString(request.body());
                    if (!TextUtils.isEmpty(body)) {
                        Follow follow = new Follow(JSON.parseObject(body, Follow.class));
                        if (!TextUtils.isEmpty(follow.getBeFollowedId())) {
                            EventBus.getDefault().post(EventAddFollow.newBuilder()
                                    .beFollowedId(follow.getBeFollowedId())
                                    .followId(respfollow.getId())
                                    .mutualFollow(follow.getMutualFollower())
                                    .build());
                        }
                    }
                } catch (Exception e) {

                }
            } else if (request.url().toString().contains("sns/userFollows") && request.method().equals("DELETE") && response.code() == ResponseCode.NO_CONTENT) {
                //处理删除关注成功
                try {
                    List<String> pathSegments = request.url().pathSegments();
                    String followId = pathSegments.get(2);
                    if (!TextUtils.isEmpty(followId)) {
                        EventBus.getDefault().post(EventDeleteFollow.newBuilder()
                                .followId(followId)
                                .build());
                    }
                } catch (Exception e) {

                }
            }
            return response;
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }


    private String bodyToString(RequestBody request) {
        try {
            RequestBody copy = request;
            Buffer buffer = new Buffer();
            if (copy != null)
                copy.writeTo(buffer);
            else
                return "";
            return buffer.readUtf8();
        } catch (Exception e) {
            return "";
        }
    }

}
